

import {downloadFile,filedownload,UploadFile} from "@/api/system";
const releasefile = {
  data() {
    return {
      fileListreleaseMixin:[],
      filenametemprelease:'',
    }
  },
  methods:{
    // 规划文本附件上传
    submitUploadreleaseMixin(param) {
      // this.$refs.upload.submit();
      const formData = new FormData()
      formData.append(param.file.uid, param.file)
      UploadFile(formData,{fileType:'3'},param).then(response => {
        if (response.data.code == 0) {
          this.projectForm.releaseId = response.data.data;
          this.fileListreleaseMixin = [{name: param.file.name, url:response.data.data}]
          this.filenametemprelease = param.file.name;
          this.$notify({
            title: "成功",
            message: "上传成功",
            type: "success",
            duration: 2000
          });
        }
      }).catch(response => {
        this.fileListreleaseMixin=[];
        this.$notify({
          title: "失败",
          message: "上传失败",
          type: "error",
          duration: 2000
        });

      })
    },
    fileChangereleaseMixin(file){
      // this.$refs.upload.clearFiles() //清除文件对象
    },

    handleRemovereleaseMixin(file, fileList){
      this.projectForm.releaseId = null;
    },
    handlePreviewreleaseMixin(file) {
      filedownload(this.projectForm.releaseId).then(response => {
        if(response.status==200){
          this.downloadMixin(response.data,this.filenametemprelease)
        }
      }).catch((error) => {

      })
    },
    // 下载文件
    downloadMixin (data,filename) {
      if (data==null) {
        return
      }else{
        let url = window.URL.createObjectURL(new Blob([data]))
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download',filename )
        document.body.appendChild(link)
        link.click()
      }
    },
    // 附件预览
    fileDownload(id,fileName){
      filedownload(id).then(response => {
        if(response.status==200){
          this.downloadMixin(response.data,fileName)
        }
      }).catch((error) => {

      })
    },
    // 上传失败弹框
    failNotify(){
      this.$notify({
        title: "失败",
        message: "上传失败",
        type: "error",
        duration: 2000
      });
    },
    // 上传成功弹框
    successNotify(){
      this.$notify({
        title: "成功",
        message: "上传成功",
        type: "success",
        duration: 2000
      });
    },
    // 附件上传方法调用（成功失败）-》多态
    fileUploadCallMethod(map,type,res,param,self){
      switch (type) {
        case 'success':
          if(map.sucMethod instanceof Function){
            map.sucMethod(res,param,self)
          }
          break;
        case 'fail':
          if(map.failMethod instanceof Function){
            map.failMethod(res,param,self)
          }
          break
        default:
          break
      }
    },
    // 附件上传
    fileUpload(param,fileType,map) {
      const formData = new FormData()
      formData.append(param.file.uid, param.file)
      UploadFile(formData,{fileType:fileType}).then(response => {
        if (response.data.code == 0) {
          this.fileUploadCallMethod(map,'success',response,param,this)
        }
      }).catch(response => {
        this.fileUploadCallMethod(map,'fail',response,param,this)
      })
    },
  }
};

export default releasefile;
